package com.atguigu.flink.chapter05.practice;


import com.atguigu.flink.bean.UserBehavior;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashSet;
import java.util.Set;

/*
uv:
    unique visitor 独立访客
 */
public class Uv {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        env.readTextFile("C:\\IDEA\\code\\LearnDemo\\flink\\input\\UserBehavior.csv")
                .map(new MapFunction<String, UserBehavior>() {
                    @Override
                    public UserBehavior map(String s) throws Exception {
                        String[] data = s.split(",");
                        return new UserBehavior(
                                Long.valueOf(data[0]),
                                Long.valueOf(data[1]),
                                Long.valueOf(data[2]),
                                data[3],
                                Long.valueOf(data[4]),
                                1L
                        );
                    }
                })
                .filter(ub -> "pv".equals(ub.getBehavior()))
                .keyBy(UserBehavior::getBehavior)
                .process(new KeyedProcessFunction<String, UserBehavior, Long>() {
                    Set<Long> userIds = new HashSet<Long>();
                    @Override
                    public void processElement(UserBehavior value,
                                               Context ctx,
                                               Collector<Long> out) throws Exception {
                        //把userId的值存入到Set集合中，set集合的长度就是uv
//                        int pre = userIds.size();
//                        userIds.add(value.getUserId());
//                        int post = userIds.size();
//                        if (post > pre){
//                            out.collect((long)userIds.size());
//                        }
                        if (userIds.add(value.getUserId())){
                            out.collect((long)userIds.size());
                        }
                    }
                })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


//public class Uv {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",2000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//        env.readTextFile("C:\\IDEA\\code\\LearnDemo\\flink\\input\\UserBehavior.csv")
//                .map(new MapFunction<String, UserBehavior>() {
//                    @Override
//                    public UserBehavior map(String s) throws Exception {
//                        String[] data = s.split(",");
//                        return new UserBehavior(
//                                Long.valueOf(data[0]),
//                                Long.valueOf(data[1]),
//                                Long.valueOf(data[2]),
//                                data[3],
//                                Long.valueOf(data[4]),
//                                1L
//                        );
//                    }
//                })
//                .filter(ub -> "pv".equals(ub.getBehavior()))
//                .keyBy(UserBehavior::getBehavior)
//                .process(new KeyedProcessFunction<String, UserBehavior, Long>() {
//                    Set<Long>  userIds = new HashSet<>();
//                    @Override
//                    public void processElement(UserBehavior value,
//                                               Context ctx,
//                                               Collector<Long> out) throws Exception {
//                        //把userIds的值放入Set集合中，set集合的长度就是uv
//                        if (userIds.add(value.getUserId())){
//                            out.collect((long)userIds.size());
//                        }
//                    }
//                })
//                .print();
//
//        try {
//            env.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}

//public class Uv {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",2000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//        env.readTextFile("C:\\IDEA\\code\\LearnDemo\\flink\\input\\UserBehavior.csv")
//                .map(new MapFunction<String, UserBehavior>() {
//                    @Override
//                    public UserBehavior map(String s) throws Exception {
//                        String[] data = s.split(",");
//                        return new UserBehavior(
//                                Long.valueOf(data[0]),
//                                Long.valueOf(data[1]),
//                                Long.valueOf(data[2]),
//                                data[3],
//                                Long.valueOf(data[0]),
//                                1L
//                        );
//                    }
//                })
//                .filter(ub -> "pv".equals(ub.getBehavior()))
//                .keyBy(UserBehavior::getBehavior)
//                .process(new KeyedProcessFunction<String, UserBehavior, Long>() {
//                    Set<Long> userIds = new HashSet<>();
//                    @Override
//                    public void processElement(UserBehavior value,
//                                               Context ctx,
//                                               Collector<Long> out) throws Exception {
//                        //把userIds的值放入set集合中，set集合的长度就是uv
//                        if(userIds.add(value.getUserId())){
//                            out.collect((long)userIds.size());
//                        }
//                    }
//                })
//                .print();
//
//        try {
//            env.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}